import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;
    public MyStack(){
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }
    //入栈操作，入到不为空的队列当中，如果都为空，入到qu1即可
    public void push(int x){
        if (!qu1.isEmpty()){
            qu1.offer(x);
        } else if (!qu2.isEmpty()) {
            qu2.offer(x);
        }else {
            qu1.offer(x);
        }
    }
    //两个队列都为空，表示栈为空
    public boolean isEmpty(){
        return qu1.isEmpty() && qu2.isEmpty();
    }
    public int pop(){
        if (isEmpty()){
            return -1;
        }
        if (!qu1.isEmpty()){
            int size = qu1.size();
            for (int i = 0; i < size-1; i++) {
                int tmp = qu1.poll();
                qu2.offer(tmp);
            }
            return qu1.poll();
        }else {
            int size = qu2.size();
            for (int i = 0; i < size-1; i++) {
                int tmp = qu2.poll();
                qu1.offer(tmp);
            }
            return qu2.poll();
        }
    }

    public int top(){
        if (isEmpty()){
            return -1;
        }
        int tmp = -1;
        if (!qu1.isEmpty()){
            int size = qu1.size();
            for (int i = 0; i < size; i++) {
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
            return tmp;
        }else {
            int size = qu2.size();
            for (int i = 0; i < size; i++) {
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
            return tmp;
        }

    }
}
